package com.yiqi.zhiyuan.feature.order.presenter;

import android.text.TextUtils;

import com.suozhang.framework.entity.bo.PageData;
import com.yiqi.zhiyuan.entity.dto.ChangeOrderDTO;
import com.yiqi.zhiyuan.entity.vo.ChangeReasonVO;
import com.yiqi.zhiyuan.entity.vo.FileVO;
import com.yiqi.zhiyuan.entity.vo.OrderVO;
import com.yiqi.zhiyuan.feature.order.contract.ChangeContract;
import com.yiqi.zhiyuan.feature.order.contract.OrderContract;
import com.yiqi.zhiyuan.feature.order.dependencies.change.DaggerChangePresenterComponent;
import com.yiqi.zhiyuan.feature.order.dependencies.order.DaggerOrderPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/14 10:54
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/14 10:54
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ChangePresenter implements ChangeContract.Presenter {

    private ChangeContract.View mView;

    @Inject
    ChangeContract.Model mModel;


    public ChangePresenter(ChangeContract.View mView) {
        this.mView = mView;
        DaggerChangePresenterComponent.create().inject(this);
    }


    @Override
    public void start() {

    }

    @Override
    public void uploadImage(String imageUrl) {
        if (TextUtils.isEmpty(imageUrl)) return;
        mModel.doUploadImage(imageUrl)
                .compose(mView.bindToLife())
                .subscribe(new Observer<FileVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FileVO fileVO) {
                        if (fileVO != null) {
                            mView.showUploadImageSuccess(fileVO.getImgUrl());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showUploadImageError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void selectChangeReason() {
        mModel.doSelectChangeReason()
                .compose(mView.bindToLife())
                .subscribe(new Observer<List<ChangeReasonVO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<ChangeReasonVO> changeReasonVOS) {
                        mView.showChangeReasonSuccess(changeReasonVOS);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showChangeReasonError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void changeOrder(int useCarType, String orderNo, String reason, String explain, List<String> imgList) {
        if (TextUtils.isEmpty(reason)) {
            mView.showChangeReasonError("请选择改派原因");
            return;
        }
        ChangeOrderDTO changeOrderDTO = new ChangeOrderDTO();
        changeOrderDTO.setUseCarType(useCarType);
        changeOrderDTO.setOrderNo(orderNo);
        changeOrderDTO.setReason(reason);
        changeOrderDTO.setExplain(TextUtils.isEmpty(explain)?null:explain);
        changeOrderDTO.setImgList(imgList);
        mModel.doChangeOrder(changeOrderDTO)
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showChangeOrderSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showChangeOrderError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
